import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/index';

import user from './modules/user'
import myCourses from './modules/myCourses'
import designCourse from './modules/designCourses'
import openCourses from './modules/openCourses'
import teacher from './modules/teacher'

/**
 * Repair that Vue test utils will report an error in the test environment use (vuerouter)
 * https://stackoverflow.com/questions/55523639/vue-test-utils-could-not-overwrite-property-route-this-is-usually-caused-by-a
 */
if (!process || process.env.NODE_ENV !== 'test') {
  Vue.use(VueRouter)
}

// 解决报错
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  // debugger
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
  // debugger
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
}

// 静态路由
export const constantRoutes = [
  {
    path: '/',
    name: 'Home',
    component: resolve => require(['@/views/home/home'], resolve),
    meta: {
      name: '首页',
      keepAlive: false,
      affix: true,
      noCache: true
    }
  },
  // {
  //   path: '/print',
  //   name: 'Print', 
  //   component: resolve => require(['@/views/print/index'], resolve),
  //   meta: {
  //     name: '报表',
  //     keepAlive: false,
  //     affix: true,
  //     noCache: true
  //   }
  // },
  // {
  //   path: '/print2',
  //   name: 'Print2', 
  //   component: resolve => require(['@/views/print/index2'], resolve),
  //   meta: {
  //     name: '报表',
  //     keepAlive: false,
  //     affix: true,
  //     noCache: true
  //   }
  // },
  // {
  //   path: "/test",
  //   name: "Test",
  //   component: resolve => require(['@/components/DanMu/danmuMessage'], resolve),
  // },
  {
    path: '/login',
    name: 'Login',
    component: resolve => require(['@/views/user/login/login'], resolve),
    meta: {
      name: '登录',
      keepAlive: false
    },
    beforeEnter: (to, from, next) => {
      // 过滤注册重置密码的路由 不记录到登录后自动跳转的地址中
      if (!to.params.routePath) {
        if (from.name && from.name !== 'Register' && from.name !== 'Resetpwd') {
          next({
            name: to.name,
            params: {
              routePath: from.path,
              query: from.query
            }
          })
        } else {
          next()
        }
      } else next()
    }
  },
  {
    path: '/reg',
    name: 'Register',
    component: resolve => require(['@/views/user/register/register'], resolve),
    meta: {
      name: '注册',
      keepAlive: false
    },
  },
  {
    path: '/rest',
    name: 'Resetpwd',
    component: resolve => require(['@/views/user/resetPassword/resetPassword'], resolve),
    meta: {
      name: '重置密码',
      keepAlive: false
    }
  },
  {
    path: '/help',
    name: 'Help',
    component: resolve => require(['@/views/help/index'], resolve),
    meta: {
      name: '帮助中心'
    }
  },
  {
    path: '/about',
    name: 'About',
    component: resolve => require(['@/views/aboutus/index'], resolve),
    meta: {
      name: '关于我们'
    }
  },
  {
    path: '/contact',
    name: 'Contact',
    component: resolve => require(['@/views/contactus/index'], resolve),
    meta: {
      name: '联系我们'
    }
  },
  {
    path: '/curr/:id',
    name: "Curr",
    meta: {
      name: "课程详情",
      keepAlive: true
    },
    component: resolve => require(['@/views/courses/detail/detail_N'], resolve),
    // component: resolve => require(['@/views/courses/detail/task_N/task_N'], resolve),
  },
  {
    path: '/browse',
    name: 'browseDetail',
    meta: {
      name: '浏览课程',
      // keepAlive: true,
    },
    component: resolve => require(['@/views/courses/browseDetail/browseDetail'], resolve),
  },
  {
    path: '/list',
    name: 'CoursesList',
    meta: {
      name: '学习广场',
      keepAlive: true,
    },
    component: resolve => require(['@/views/courses/list/courseslist'], resolve),
  },
  {
    path: '/mall',
    name: 'Mall',
    meta: {
      name: '商城',
      keepAlive: true,
    },
    component: resolve => require(['@/views/mall/index'], resolve),
  },
  {
    path: '/mall/detail/:id',
    name: 'MallDetail',
    meta: {
      name: '商品详情',
      keepAlive: true,
    },
    component: resolve => require(['@/views/mall/detail/index'], resolve),
  },
  openCourses
]

// 异步路由
export const asyncRoutes = [
  user,
  myCourses,
  designCourse,
  // openCourses,
  teacher
]

const createRouter = () => new VueRouter({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router